package com.softer.wxzj.common.util;

import lombok.extern.slf4j.Slf4j;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
@Slf4j
public class DateUtils {
    public static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_PATTERN = "yyyy-MM-dd";
    public static final String MM_DD = "MM-dd";
    public static final String MM = "MM";
    public static final String YYYY = "yyyy";
    public static final String DATE_YYYY_MM = "yyyy-MM";
    public static String getNow() {
        DateFormat df = new SimpleDateFormat(DATE_TIME_PATTERN);
        return df.format(new Date());
    }

    public static String getYyyyMm() {
        DateFormat df = new SimpleDateFormat(DATE_YYYY_MM);
        return df.format(new Date());
    }
    public static String getMmDdNow() {
        DateFormat df = new SimpleDateFormat(MM_DD);
        return df.format(new Date());
    }
    public static String getMmNow() {
        DateFormat df = new SimpleDateFormat(MM);
        return df.format(new Date());
    }

    public static String getYYYYNow() {
        DateFormat df = new SimpleDateFormat(YYYY);
        return df.format(new Date());
    }

    public static String getDateNow() {
        DateFormat df = new SimpleDateFormat(DATE_PATTERN);
        return df.format(new Date());
    }

    public static String getDay(String day) {
        Calendar c = Calendar.getInstance();
        Date date = null;
        try {
            date = new SimpleDateFormat("yy-MM-dd").parse(day);
        } catch (ParseException e) {
            log.error("ParseException错误信息:()", e.getMessage(), e);
        }
        c.setTime(date);
        int day1 = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day1 + 1);

        return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
    }

    /**
     * 校验传入的时间与当前时间的关系，如果传入的时间是当天或者在当天之前，返回true，否则返回false
     *
     * @param time 校验的时间
     * @return 校验结果
     */
    public static Boolean checkTime(String time) {
        try {
            Date parse = new SimpleDateFormat("yyyy-MM-dd").parse(time);
            if (parse.before(new Date())) {
                return true;
            }
        } catch (ParseException e) {
            log.error("ParseException错误信息:()", e.getMessage(), e);
        }
        return false;
    }

    /**
     * 获取n天后的日期
     *
     * @param num n天
     * @return 日期
     */
    public static LocalDate getDate(int num) {
        LocalDate today = LocalDate.now();
        return today.plusDays(num);
    }
}
